package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.BlogComentsDto;
import com.hmdp.dto.Result;
import com.hmdp.entity.BlogComments;
import com.hmdp.service.IBlogCommentsService;
import com.hmdp.utils.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog-comments")
public class BlogCommentsController {
 @Autowired
    private IBlogCommentsService blogCommentsService;

    @PostMapping("/saveComments")
    public Result saveComments(@RequestBody BlogComments shop){
        return blogCommentsService.saveComments(shop);
    }

    @GetMapping("/of/blog")
    public Result queryCommentsByShopId(@RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long shopId) {
        // 根据用户查询
        Page<BlogComments> page = blogCommentsService.query()
                .eq("blog_id", shopId).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<BlogComments> records = page.getRecords();
        BlogComentsDto shopCommentsDto=new BlogComentsDto();
        shopCommentsDto.setBlogComments(records);
        shopCommentsDto.setCount(records.size());
        return Result.ok(shopCommentsDto);
    }
}
